<?php

class MessageSent extends AppModel {
    var $name = 'MessageSent';

    var $useTable = 'messages_sent';

    var $order = 'MessageSent.id DESC';

    var $belongsTo = array(
        'FromSupplier' => array(
            'foreignKey' => 'sender_id',
            'conditions' => array(
                'sender' => SUPPLIER_PREFIX,
            )
        ),
        'FromCustomer' => array(
            'foreignKey' => 'sender_id',
            'conditions' => array(
                'sender' => CUST_PREFIX
            )
        ),
        'ToSupplier' => array(
            'foreignKey' => 'receiver_id',
            'conditions' => array(
                'receiver' => SUPPLIER_PREFIX
            )
        ),
        'ToCustomer' => array(
            'foreignKey' => 'receiver_id',
            'conditions' => array(
                'receiver' => CUST_PREFIX
            )
        ),
        'User'
    );

    var $validate = array(
        'sender' => array(
            'rule' => 'notEmpty',
            'message' => 'From?',
            'on' => 'create'
        ),
        'receiver' => array(
            'rule' => 'notEmpty',
            'message' => 'receiver?',
            'on' => 'create'
        ),
        'user_id' => array(
            'rule' => 'notEmpty',
            'message' => 'user?',
            'on' => 'create'
        ),
        'sent_time' => array(
            'rule' => 'notEmpty',
            'message' => 'sent_time?',
            'on' => 'create'
        ),
        'subject' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input a subject.',
            'on' => 'create'
        ),
        'body' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input some message.',
            'on' => 'create'
        ),
    );


    function afterFind($results) {
        foreach ($results as &$mr) {
            if (isset($mr['MessageSent']['sent_time'])) {
                $mr['MessageSent']['sent_time'] = $this->datetimeFormatAfterFind($mr['MessageSent']['sent_time']);
            }
        }
        return $results;
    }
}
?>
